﻿
#region Add-Accelerator 

function Add-Accelerator 
{
    <#
    .Synopsis
        Add a type accelerator to the current session
    .Description
        The Add-Accelerator function allows you to add a simple type accelerator (like [regex]) for a longer type (like
    [System.Text.RegularExpressions.Regex]).
    .Example
        Add-Accelerator list System.Collections.Generic.List``1
        $list = New-Object list[string]
        
        Creates an accelerator for the generic List[T] collection type, and then creates a list of strings.
    .Example
        Add-Accelerator "List T", GList System.Collections.Generic.List``1
        $list = New-Object "list t[string]"
        
        Creates two accelerators for the Generic List[T] collection type.
    .Parameter Accelerator
        The short form accelerator should be just the name you want to use (without square brackets).
    .Parameter Type
        The type you want the accelerator to accelerate (without square brackets)
    .Notes
        When specifying multiple values for a parameter, use commas to separate the values. 
        For example, "-Accelerator string, regex".
        
        PowerShell requires arguments that are "types" to NOT have the square bracket type notation, because of the way
    the parsing engine works.  You can either just type in the type as System.Int64, or you can put parentheses around it 
    to help the parser out: ([System.Int64])
    
        Also see the help for Get-Accelerator and Remove-Accelerator
    .Link
        http://huddledmasses.org/powershell-2-ctp3-custom-accelerators-finally/
      
#>
    [CmdletBinding()]
    PARAM(
        [Parameter(Position=0)]
        [Alias("Key")]
        [string[]]$Accelerator,
        
        [Parameter(Position=1)]
        [Alias("Value")]
        [type]$Type
    )
    
    process {
    
        # add a user-defined accelerator  
        foreach($a in $Accelerator) 
        { 
            $xlr8r::Add( $a, $Type) 
            trap [System.Management.Automation.MethodInvocationException] {
                if($xlr8r::get.keys -contains $a) {
                    Write-Error "Cannot add accelerator [$a] for [$($Type.FullName)]`n                  [$a] is already defined as [$($xlr8r::get[$a].FullName)]"
                    Continue;
                } 
                throw
            }
        }
        
    }
}
    
#endregion 


#region Disable-RemoteDesktop 

function Disable-RemoteDesktop
{
        
    <#
        .Synopsis 
            Disables Remote Desktop Setting.
            
        .Description
            Disables Remote Desktop Setting.
            
        .Parameter ComputerName
            Computer to get the setting from (Default to localhost.)
            
        .Example
            Disable-RemoteDesktop 
            Description
            -----------
            Disables local Remote Desktop Setting 
            
        .Example
            Disable-RemoteDesktop -ComputerName MyComputer
            Description
            -----------
            Disables Remote Desktop Setting on MyComputer
            
        .OUTPUTS
            PSCustomObject
            
        .Link
            Get-RemoteDesktop
            Enable-RemoteDesktop
            
        .Notes
            NAME:      Disable-RemoteDesktop
            AUTHOR:    YetiCentral\bshell
            Website:   www.bsonposh.com
            #Requires -Version 2.0
    #>
    
    [Cmdletbinding(SupportsShouldProcess=$true)]
    Param(
        [Alias("Server")]
        [Parameter(ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)]
        [string]$ComputerName = $Env:ComputerName
    )
    
    Process 
    {
    
        if($ComputerName -match "(.*)(\$)$")
        {
            $ComputerName = $ComputerName -replace "(.*)(\$)$",'$1'
        }
        if(Test-Host -ComputerName $ComputerName)
        {
            if($PSCmdlet.ShouldProcess($ComputerName,"Disabling Remote Desktop"))
            {
                Set-RegistryValue -ComputerName $ComputerName `
                                -Path "HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server" `
                                -Name fDenyTSConnections    `
                                -Value 1                    `
                                -Type DWord
                Set-RegistryValue -ComputerName $ComputerName `
                                -Path "HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server" `
                                -Name AllowTSConnections    `
                                -Value 0                    `
                                -Type DWord
            }
            Get-RemoteDesktop -ComputerName $ComputerName
        }
        else
        {
            Write-Host "Unable to Ping $ComputerName"
        }
        
    }
}
    
#endregion 


#region Enable-RemoteDesktop 

function Enable-RemoteDesktop
{
    
    <#
        .Synopsis 
            Enables Remote Desktop Setting.
            
        .Description
            Enables Remote Desktop Setting.
            
        .Parameter ComputerName
            Computer to get the setting from (Default to localhost.)
            
        .Example
            Enable-RemoteDesktop 
            Description
            -----------
            Enables local Remote Desktop Setting 
            
        .Example
            Enable-RemoteDesktop -ComputerName MyComputer
            Description
            -----------
            Enables Remote Desktop Setting on MyComputer
            
        .OUTPUTS
            PSCustomObject
            
        .Link
            Get-RemoteDesktop
            Disable-RemoteDesktop
            
        .Notes
            NAME:      Get-RemoteDesktop
            AUTHOR:    YetiCentral\bshell
            Website:   www.bsonposh.com
            #Requires -Version 2.0
    #>
    
    [Cmdletbinding(SupportsShouldProcess=$true)]
    Param(
        [Alias("Server")]
        [Parameter(ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)]
        [string]$ComputerName = $Env:ComputerName
    )
    
    Process 
    {
        if($ComputerName -match "(.*)(\$)$")
        {
            $ComputerName = $ComputerName -replace "(.*)(\$)$",'$1'
        }
        if(Test-Host -ComputerName $ComputerName)
        {
            if($PSCmdlet.ShouldProcess($ComputerName,"Enabling Remote Desktop"))
            {
                Set-RegistryValue -ComputerName $ComputerName `
                                -Path "HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server" `
                                -Name fDenyTSConnections    `
                                -Value 0                    `
                                -Type DWord
                Set-RegistryValue -ComputerName $ComputerName `
                                -Path "HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server" `
                                -Name AllowTSConnections    `
                                -Value 1                    `
                                -Type DWord
            }
            Get-RemoteDesktop -ComputerName $ComputerName
        }
        else
        {
            Write-Host "Unable to Ping $ComputerName"
        }
    
    }
}
    
#endregion 


#region Get-RemoteDesktop 

function Get-RemoteDesktop
{
    
    <#
        .Synopsis 
            Gets the value of Remote Desktop Setting.
            
        .Description
            Gets the value of Remote Desktop Setting.
            
        .Parameter ComputerName
            Computer to get the setting from (Default to localhost.)
            
        .Example
            Get-RemoteDesktop 
            Description
            -----------
            Gets local Remote Desktop Setting 
            
        .Example
            Get-RemoteDesktop -ComputerName MyComputer
            Description
            -----------
            Gets Remote Desktop Setting on MyComputer
        
        .OUTPUTS
            PSCustomObject
            
        .Link
            Enable-RemoteDesktop
            Disable-RemoteDesktop
            
        .Notes
            NAME:      Get-RemoteDesktop
            AUTHOR:    YetiCentral\bshell
            Website:   www.bsonposh.com
            LASTEDIT:  11/03/2009 
            #Requires -Version 2.0
    #>
    
    [Cmdletbinding()]
    Param(
        [Alias("Server")]
        [Parameter(ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)]
        [string]$ComputerName = $Env:ComputerName
    )
    
    Begin
    {
        $TSKey = "HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server"
    }
    
    Process
    {
    
        if($ComputerName -match "(.*)(\$)$")
        {
            $ComputerName = $ComputerName -replace "(.*)(\$)$",'$1'
        }
        if(Test-Host -ComputerName $ComputerName)
        {
            $DenyKey = Get-RegistryValue -ComputerName $ComputerName -Path $TSKey -Name fDenyTSConnections
            $AllowKey = Get-RegistryValue -ComputerName $ComputerName -Path $TSKey -Name AllowTSConnections
            if(($DenyKey.Value -eq 0) -or ($AllowKey.Value -eq 1))
            {
                $Enabled = $true
            }
            else
            {
                $Enabled = $false
            }
            
            $myobj = @{
                ComputerName         = $ComputerName
                DenyTSConnections    = $DenyKey.Value
                AllowTSConnections   = $AllowKey.Value
                RemoteDesktopEnabled = $Enabled
            }
            
            $obj = New-Object PSObject -Property $myobj
            $obj.PSTypeNames.Clear()
            $obj.PSTypeNames.Add('BSonPosh.RemoteDesktop.State')
            $obj
        }
        else
        {
            Write-Host "Unable to Ping $ComputerName"
        }
    
    }
}
    
#endregion 

